package com.lyh.db137.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyh.db137.common.R;
import com.lyh.db137.entity.Menu;
import com.lyh.db137.entity.Rolemenu;
import com.lyh.db137.service.MenuService;
import com.lyh.db137.service.RolemenuService;
import com.lyh.db137.common.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "*")
public class MenuController {
    /**
     * 服务对象
     */
    @Autowired
    private MenuService menuService;
    @Autowired
    private RolemenuService rolemenuService;
    @Autowired
    private RolemenuController rolemenuController;
    //查询用户菜单
    @GetMapping("/selectMenuByUserId")
    public R selectMenuByUserId(@RequestHeader("token") String token) {
        // 解析 token，获取用户 id
        Integer userId = JwtUtils.getJwtToken(token, "userid");
        System.out.println("userId = " + userId);

        List<Menu> menus = menuService.selectMenuByUserId(userId);
        return new R().addData("menus", menus);
    }


    //查询所有菜单
    @GetMapping("/selectallmenu")
    public R selectAllMenu() {
        List<Menu> menus = menuService.list();
        return new R().addData("menus", menus);
    }

    //根据id查询菜单
    @GetMapping("/selectmenuById")
    public R selectMenuById(Integer id) {
        Menu menu = menuService.getById(id);
        return new R().addData("menu", menu);
    }

    //根据菜单名模糊查询菜单
    @GetMapping("/selectmenuByName")
    public R selectMenuByName(String label) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("label", label);
        List<Menu> menus = menuService.list(queryWrapper);
        return new R().addData("menus", menus);
    }

    //根据id查询菜单的parentid
    @GetMapping("/selectmenuParentId")
    public int selectmenuParentId(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        List<Menu> menus = menuService.list(queryWrapper);
        return menus.get(0).getParentid();
    }

    //删除
    @PostMapping("/deletemenu")
    public R deleteMenu(Integer id) {
        boolean b = menuService.removeById(id);
        return new R().addData("b", b);
    }

    //添加
    @PostMapping("/addmenu")
    public R addMenu(@RequestBody Menu menu) {
        boolean b = menuService.save(menu);
        if (b) {
            QueryWrapper queryWrapper = new QueryWrapper<>();
            //查询id最大值
            queryWrapper.select("max(id) id");
            Menu one =menuService.getOne(queryWrapper);
            System.out.println("one = " + one.getId());

            try{
                rolemenuService.save(new Rolemenu(0,
                        rolemenuController.selectRolemenu(this.selectmenuParentId(one.getId())),
                        one.getId()));
            }catch (Exception e){
                System.out.println("\n暂无职位有相关业务\n");
            }
        }
        return new R().addData("b", b);
    }

    //修改
    @PostMapping("/updatemenu")
    public R updateMenu(@RequestBody Menu menu) {
        boolean b = menuService.updateById(menu);
        return new R().addData("b", b);
    }
}
